package com.card.hwatt.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.hwatt.assembler.ScheduleAssembler;
import com.card.hwatt.core.RequestJsonParam;
import com.card.hwatt.dto.PageParam;
import com.card.hwatt.dto.ReturnValue;
import com.card.hwatt.dto.Schedule;
import com.card.hwatt.service.ScheduleService;

/**
 * 班次
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/schedule")
public class ScheduleController extends BaseController{

	@Autowired
	@Qualifier("scheduleService")
	private ScheduleService scheduleService;

	@RequestMapping("/index")
	public String index() {
		return "schedule";
	}

	@RequestMapping("/save")
	public @ResponseBody
	ReturnValue save(@RequestJsonParam Schedule schedule) {
		scheduleService.save(ScheduleAssembler.convert(schedule));

		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}

	@RequestMapping("/delete")
	public @ResponseBody
	ReturnValue delete(@RequestParam UUID id) {
		scheduleService.delete(id);
		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}

	@RequestMapping("/loadAll")
	public @ResponseBody
	List<Schedule> loadAll(Long id) {
		if (id == null) {
			List<Schedule> result = ScheduleAssembler.convert(scheduleService
					.loadAll(new PageParam(0, 0, " department is null ",
							new ArrayList<Object>(), "name")).rows);
			return result;
		}
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		List<Schedule> result = ScheduleAssembler.convert(scheduleService
				.loadAll(new PageParam(0, 0,
						" department is not null and department.id = ? ",
						params, "name")).rows);
		return result;
	}
}
